﻿#include "PrinterManager.h"

PrinterManager::PrinterManager(QObject* parent) {}

PrinterManager::~PrinterManager() {}

QString PrinterManager::createHtml(DaoModel::TestRecord& record) {
	QStringList html;
	html
		<< tr("<!DOCTYPE html>")
		<< tr("<html lang='en'>")
		<< tr("<head>")
		<< tr("<meta content='text/html; charset=UTF-8'>")
		<< tr("</head>")
		<< tr("<body>")
		<< tr("<h1 style='text-align:center'>%1检测数据</h1>").arg(Configuration::getInstance().equipment.chName)
		//检测信息
		<< tr("<div style='margin: 0 10px'>")
		<< tr("<table border='0' cellspacing='0' cellpadding='0' width='100%' style='margin-left:0;border:none; border-collapse:collapse; font-size:14px'>")
		<< tr("<tr style='border: none'>")
		<< tr("<td style='text-align: left; border: none '>批次: %1</td>").arg(record.testNum())
		<< tr("<td style='text-align: left; border: none '></td>")
		<< tr("</tr>")
		<< tr("<tr style='border: none'>")
		<< tr("<td style='text-align: left; border: none'>牌号: %1</td>").arg(record.brand())
		<< tr("<td style='text-align: left; border: none'>班次: %1</td>").arg(record.schedule())
		<< tr("</tr>")
		<< tr("<tr style='border:none;'>")
		<< tr("<td style='text-align: left; border: none'>检测员: %1</td>").arg(record.tester())
		<< tr("<td style='text-align: left; border: none '>检测时间: %1</td>").arg(record.testTime().toString(DATETIME_FORMAT))
		<< tr("</tr>")
		<< tr("</table>")
		<< tr("</div>")
		//检测数据
		<< tr("<div style='width:100%'>")
		<< tr("<table border='1' cellspacing='0' cellpadding='0' width='100%' style='border-collapse:collapse; font-size:16px;'>")
		<< tr("<tr>")
		<< tr("<th style='text-align:center;'>编号</th>")
		<< tr("<th style='text-align:center;'>烟支面积(mm²)</th>")
		<< tr("<th style='text-align:center;'>警语区面积(mm²)</th>")
		<< tr("<th style='text-align:center;'>分割线宽(mm)</th>")
		<< tr("<th style='text-align:center;'>文字高度(mm)</th>")
		<< tr("<th style='text-align:center;'>文字面积(mm²)</th>")
		<< tr("<th style='text-align:center;'>警语区占比(%)</th>")
		<< tr("<th style='text-align:center;'>文字占比(%)</th>")
		<< tr("</tr>")
		<< insertData(record)
		<< tr("</table><br/>")
		<< tr("<p style='text-align:right; margin-right:50px; margin-top:0px;'>打印日期:%1</p>").arg(QDateTime::currentDateTime().toString("yyyy/MM/dd HH:mm"))
		<< tr("</div>")
		<< tr("</body>")
		<< tr("</html>");
	return html.join("");
}

QString PrinterManager::insertData(DaoModel::TestRecord& record) {
	QStringList html;
	QList<DaoModel::TestData>& dataList = record.dataList();
	for (int i = 0; i < dataList.size(); i++) {
		html
			<< tr("<tr>")
			<< tr("<td style='text-align:center;'>%1</td>").arg(dataList[i].num())
			<< tr("<td style='text-align:center;'>%1</td>").arg(QString::number(dataList[i].area(), 'f', 3))
			<< tr("<td style='text-align:center;'>%1</td>").arg(QString::number(dataList[i].warningArea(), 'f', 3))
			<< tr("<td style='text-align:center;'>%1</td>").arg(dataList[i].lineWidth())
			<< tr("<td style='text-align:center;'>%1</td>").arg(dataList[i].textHeight())
			<< tr("<td style='text-align:center;'>%1</td>").arg(QString::number(dataList[i].textArea(), 'f', 3))
			<< tr("<td style='text-align:center;'>%1</td>").arg(QString::number(dataList[i].warningAreaRatio(), 'f', 2))
			<< tr("<td style='text-align:center;'>%1</td>").arg(QString::number(dataList[i].textAreaRatio(), 'f', 2))
			<< tr("</tr>");
	}
	return html.join("\n");
}

void PrinterManager::printDirect(DaoModel::TestRecord& record) {
	QPrinter printer(QPrinter::ScreenResolution);
	//设置输出格式
	printer.setOutputFormat(QPrinter::NativeFormat);
	//设置页边距
	printer.setPageMargins(0, 0, 0, 0, QPrinter::Millimeter);
	printer.setPageSize(QPrinter::A4);
	QPrintDialog printDialog(&printer);
	if (printDialog.exec() == QDialog::Accepted){
		QTextDocument document;
		document.setPageSize(QSizeF(printer.pageRect().size()));
		document.setDocumentMargin(30);
		QString html = createHtml(record);
		document.setHtml(html);
		document.print(&printer);
	}
}